/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Operations.Bit;

import Operations.*;
import hep.aida.IHistogram;
import hep.aida.IHistogram1D;
import java.awt.image.BufferedImage;

/**
 *
 * @author littlechild
 */
public class OpEqualization implements Operation{

    public BufferedImage operate(BufferedImage image, double[] paras) {
        //get the histogram for three colors
        Operations.CmputHistogramInMode cmp=new CmputHistogramInMode();
        cmp.setMode("Gray");
        IHistogram1D hisGray=(IHistogram1D)cmp.compute(image);
        //compute the function for translating
        int funcGray[]=new int[256];
        int caculaterGray=0;
        double a=255.0/(hisGray.allEntries());
        for(int i=0;i!=256;++i) {
            caculaterGray+=hisGray.binEntries(i);
            funcGray[i]=(int)(caculaterGray*a);
        }        
        //apply the function to each pixel on the graph
        System.out.println(image.getWidth()+" "+image.getHeight());
        BufferedImage returnImage=new BufferedImage(image.getWidth(),image.getHeight(),image.getType());
        for(int i=0;i!=image.getWidth();++i) {
            for(int j=0;j!=image.getHeight();++j) {
                //System.out.println("i:"+i+";j"+j);
                int rgb=image.getRGB(i, j);
                int red=(rgb>>>16)&0xff;
                int green=(rgb>>>8)&0xff;
                int blue=rgb&0xff;
                returnImage.setRGB(i, j, (funcGray[red]<<16)|(funcGray[green]<<8)|funcGray[blue]);
            }
        }
        return returnImage;
    }

}
